package com.blacktry.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.*;
import com.blacktry.entity.model.request.PaperRequest;
import com.blacktry.service.*;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷管理表 前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/paperManage")
@Api(value = "试卷模块", tags = "试卷管理接口")
public class PaperManageController {
    @Autowired
    private PaperManageService paperManageService;

    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @Autowired
    private MultiQuestionService multiQuestionService;

    @Autowired
    private FillQuestionService fillQuestionService;

    @Autowired
    private SubjectiveQuestionService subjectiveQuestionService;


    @OperLog(operModul = "选择题模块-查询所有",operType = OprLogConst.SELECT,operDesc = "查询所有")
    @ApiOperation("查询所有")
    @GetMapping("/papers")
    public ApiResult<PaperManage> selectAll() {
        return  ApiResultHandler.buildApiResult(200,"请求成功",paperManageService.selectAll());
    }


    @OperLog(operModul = "选择题模块-查询所有试卷编码",operType = OprLogConst.SELECT,operDesc = "查询所有试卷编码")
    @ApiOperation("查询所有试卷编码")
    @GetMapping("/papersIds")
    public ApiResult<Integer> findAllIds() {
        return  ApiResultHandler.buildApiResult(200,"请求成功",paperManageService.findAllIds());
    }

    @OperLog(operModul = "选择题模块-分页查询所有试卷编码",operType = OprLogConst.SELECT,operDesc = "分页查询所有试卷编码")
    @ApiOperation("分页查询所有试卷编码")
    @PostMapping("/papersIds/{page}/{size}")
    public ApiResult<Page<Integer>> findAllIds(@RequestBody PaperRequest paperRequest, @DefaultValue("1") @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size) {
        Page<PaperManage> paperManagePage = new Page<>(page,size);
        return  ApiResultHandler.buildApiResult(200,"请求成功",paperManageService.findAllIdsPage(paperRequest,paperManagePage));
    }

    @OperLog(operModul = "选择题模块-查询具体试卷",operType = OprLogConst.SELECT,operDesc = "查询具体试卷")
    @ApiOperation("查询具体试卷")
    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId) {
        List<MultiQuestion> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);   //选择题题库 1
        List<FillQuestion> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);     //填空题题库 2
        List<JudgeQuestion> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);   //判断题题库 3
        List<SubjectiveQuestion> subjectiveQuestionRes = subjectiveQuestionService.findByIdAndType(paperId);   //主观题题库 4
        Map<Integer, List<?>> map = new HashMap<>();
        map.put(1,multiQuestionRes);
        map.put(2,fillQuestionsRes);
        map.put(3,judgeQuestionRes);
        map.put(4,subjectiveQuestionRes);
        return map;
    }

    @OperLog(operModul = "选择题模块-添加",operType = OprLogConst.ADD,operDesc = "添加")
    @ApiOperation("添加")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody PaperManage paperManage) {
        int res = paperManageService.insert(paperManage);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    @OperLog(operModul = "选择题模块-删除指定",operType = OprLogConst.DELETE,operDesc = "删除指定")
    @ApiOperation("删除指定")
    @DeleteMapping("/delete/{questionType}/{paperId}/{questionId}")
    public ApiResult<Integer> delete(@PathVariable Integer paperId,@PathVariable Integer questionId,@PathVariable Integer questionType){
        return ApiResultHandler.buildApiResult(200,"成功",paperManageService.delete(paperId,questionId,questionType));
    }

    @OperLog(operModul = "选择题模块-批量删除",operType = OprLogConst.DELETE,operDesc = "批量删除")
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatchIds/{questionType}/{paperId}")
    public ApiResult<Integer> deleteBatchIds(@PathVariable Integer paperId,String[] ids,@PathVariable Integer questionType){
        return ApiResultHandler.buildApiResult(200,"成功",paperManageService.deleteBatchIds(paperId,ids,questionType));
    }

}

